package com.hidt.shop.service.impl;

import com.hidt.shop.domain.Seckil;
import com.hidt.shop.mapper.GoodsMapper;
import com.hidt.shop.mapper.SeckilMapper;
import com.hidt.shop.service.SeckilService;
import com.hidt.shop.util.RestDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author Fhy
 * @Title SeckilServiceImpl
 * @Package com.hdit.shop.service.impl
 * @Description 商品秒杀表的业务处理实现类
 * @date 2021/1/21 15:55
 */
@Service
public class SeckilServiceImpl implements SeckilService {
    @Autowired
    private SeckilMapper seckilMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public RestDate addSeckil(Seckil seckil) {
        int j = goodsMapper.updataGoodsSeckilDiscount(seckil.getGoods_id(), seckil.getSeckil_discount());
        System.out.println("秒杀折扣修改： " + j);
        int i = seckilMapper.addSeckil(seckil);
        RestDate restDate = new RestDate();
        if (i == 1) {
            restDate.setCode(200);
            restDate.setMess(judgement(i,"秒杀区商品添加"));
        } else {
            restDate.setCode(101);
            restDate.setMess(judgement(i,"秒杀区商品添加"));
        }

        return restDate;
    }

    @Override
    public String deleteSeckil(int seckil_id, int goods_id) {
        int j = goodsMapper.updataGoodsSeckilDiscount(goods_id, -1);
        int i = seckilMapper.deleteSeckil(seckil_id);
        return judgement(i,"秒杀区商品删除");
    }

    @Override
    public String updateSeckil(Seckil seckil) {
        int i = seckilMapper.updateSeckil(seckil);
        return judgement(i,"秒杀区商品修改");
    }

    @Override
    public int purchaseSeckil(Map<String, Integer> map) {
        int i = seckilMapper.purchaseSeckil(map);
        return i;
    }

    @Override
    public List<Seckil> findAllSeckil(int seckiltime_id) {
        List<Seckil> seckilList = seckilMapper.findAllSeckil(seckiltime_id);
        return seckilList;
    }
    private String judgement(int i, String str) {
        if(i == 1) {
            return str + "成功";
        } else {
            return str + "失败";
        }
    }
}
